//import com.adobe.serialization.json.JSON;

import events.MyEvent;

import filemanager.FileScaner;
import filemanager.FolderClass;

import flash.desktop.ClipboardFormats;
import flash.desktop.DockIcon;
import flash.desktop.InteractiveIcon;
import flash.desktop.NativeApplication;
import flash.desktop.NativeDragManager;
import flash.desktop.SystemTrayIcon;
import flash.display.Loader;
import flash.display.NativeWindow;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.NativeDragEvent;
import flash.events.TimerEvent;
import flash.filesystem.File;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.xml.XMLNode;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.FlexNativeMenu;
import mx.events.FlexEvent;
import mx.events.FlexNativeMenuEvent;
import mx.managers.PopUpManager;

import org.osmf.events.TimeEvent;

import views.*;

[Bindable]
public var fileFilter:Array=new Array();
[Bindable]
public var fileArray:Array=new Array();
public var loading_mc:LoadingWindow;
private var fileScaner:FileScaner;
private var folderObject:FolderClass=new FolderClass();
private var iconMenu:FlexNativeMenu; 
private var icon:Loader = new Loader();
private var currentFile:File;
private var newXML:XML;

private function init():void
{
	addFolderDrageEvent();
	NativeApplication.nativeApplication.autoExit = true;
	iconMenu = new FlexNativeMenu(); 
	iconMenu.dataProvider = menuData; 
	iconMenu.labelField = "@label"; 
	iconMenu.showRoot = false; 

	if (NativeApplication.supportsSystemTrayIcon) {
		
		icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);
		
		icon.load(new URLRequest("images/128.png"));
		this.systemTrayIconMenu = iconMenu;
		this.systemTrayIconMenu.addEventListener(FlexNativeMenuEvent.ITEM_CLICK,systemExit);
	}

	if (NativeApplication.supportsDockIcon){
		icon.contentLoaderInfo.addEventListener(Event.COMPLETE,iconLoadComplete);
		icon.load(new URLRequest("images/128.png"));
		
		this.dockIconMenu = iconMenu;
	}
}



private function updateFileList():void
{
	if(currentFile==null)return;
	if(fileArray)
	{
		fileArray=null;
		folderObject=null;
	}
	
	fileArray=new Array();
	fileScaner=new FileScaner();
	fileScaner._filters=this.fileFilter;
	folderObject=new FolderClass(currentFile.nativePath);
	folderObject.children=fileScaner.scanFolder(new File(currentFile.nativePath));
	fileArray.push(folderObject);
}

private function handlexmljsonChange(e:MyEvent):void
{
	updateFileList();
}

private function handleTypeFilterChange(e:MyEvent):void
{
	this.fileFilter=e.obj;
	updateFileList();
}

private function addFolderDrageEvent():void
{
	this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
	this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
}

private function onDragIn(e:NativeDragEvent):void
{
	var _clip:Clipboard=e.clipboard;
	var dropfiles:Array= _clip.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
	var _currentFile:File = dropfiles[0];
	
	
	if(_currentFile.isDirectory )
	{
		NativeDragManager.acceptDragDrop(this);
	}

}

private function onDrop(e:NativeDragEvent):void
{

	var dropfiles:Array= e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
	currentFile = dropfiles[0];
	
	//loading_mc= LoadingWindow(PopUpManager.createPopUp(this,LoadingWindow,true)); 
	
	//loading_mc.addEventListener(FlexEvent.UPDATE_COMPLETE,updateFiles);
	//PopUpManager.centerPopUp(loading_mc);
	outputview.visible=false;
	this.updateFileList();
}


private function updateFiles(e:FlexEvent):void
{
	//loading_mc.closeButton.visible=false;
	if(fileArray)
	{
		fileArray=null;
		folderObject=null;
	}
	
	fileArray=new Array();
	fileScaner=new FileScaner();
	folderObject=new FolderClass(currentFile.nativePath);
	folderObject.children=fileScaner.scanFolder(new File(currentFile.nativePath));
	fileArray.push(folderObject);
	
	//loading_mc.removeEventListener(FlexEvent.UPDATE_COMPLETE,updateFiles);
}

private function handleTreeDataComplete(e:Event):void
{
	outputview.visible=true;
	/*if(fileArray.length>0)
	{
		var ss:Timer=new Timer(200,1);
		ss.addEventListener(TimerEvent.TIMER_COMPLETE,handleTimerEvent);
		ss.start();
	}*/
	setData();
}

private function saveFile(e:MyEvent):void
{
	if(outputview.xmldata.text.length>0)
	{
		var lastString:String;
		switch(e.obj)
		{
			case "xml":
				lastString=".xml";
				break;
			case "json":
				lastString=".txt";
		}
	}
	fileReference.save(outputview.xmldata.text, lastString);
}

private function setData():void
{
	outputview.xmldata.text="";
	if(this.fileArray.length==0)return;
	
	if(this.setting.fileType=="xml" )
	{
		makeXml();
	}
	else
	{
		makeJson();
	}
}

private function makeJson():void
{
	
	var str:String=JSON.stringify(this.fileArray);
	trace(str);
	outputview.xmldata.text=str;
}

private function makeXml():void
{
	newXML=new XML(makeFolder(this.fileArray[0]).toString());
	outputview.xmldata.text=newXML;
	//trace(makeFolder(this.fileArray[0]).toString());
}

private function makeFolder(s:FolderClass):XMLNode
{
	var node:XMLNode;
	

	node = new XMLNode(1,"folder");
	node.attributes.name=s.label;
	if (s.children != null)
	{
		var str:String;
		for (var i:uint=0; i<s.children.length; i++)
		{
			var _file:FolderClass = s.children[i] as FolderClass;
			
			if (_file.isFolder == 0)
			{
				if (_file.children.length > 0)
				{
					node.appendChild(makeFolder(_file));
				}
			}
			else
			{
				node.appendChild(makeFile(_file));
			}
			
		}
	}
	else
	{
		node.appendChild(makeFile(s));
	}
	
	return node;
}

private function makeFile(s:FolderClass):XMLNode
{
	var node_0:XMLNode=new XMLNode(1,"file");
	var node_1:XMLNode=new XMLNode(1,"name");
	var node_2:XMLNode=new XMLNode(3,s.label);
	var node_3:XMLNode=new XMLNode(1,"path");
	var node_4:XMLNode=new XMLNode(3,s.path);
	
	node_0.appendChild(node_1);
	node_1.appendChild(node_2);
	node_0.appendChild(node_3);
	node_3.appendChild(node_4);

	
	return node_0;
}

private function handleTimerEvent(e:TimerEvent):void
{
	PopUpManager.removePopUp(loading_mc);
}


private function systemExit(e:FlexNativeMenuEvent):void
{
	
	NativeApplication.nativeApplication.icon.bitmaps = [];
	NativeApplication.nativeApplication.exit();
	
}

private function iconLoadComplete(event:Event):void
{
	NativeApplication.nativeApplication.icon.bitmaps =[event.target.content.bitmapData];
}

